﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace YunxSoft.Abp.LanguageManagement.EntityFrameworkCore;

public static class LanguageManagementDbContextModelCreatingExtensions
{
    public static void ConfigureLanguageManagement(
        this ModelBuilder builder)
    {
        Check.NotNull(builder, nameof(builder));

        builder.Entity<Language>(b =>
        {
            b.ToTable(LanguageManagementDbProperties.DbTablePrefix + "Languages", LanguageManagementDbProperties.DbSchema);
            b.ConfigureByConvention();
            b.Property(x => x.CultureName).IsRequired().HasMaxLength(LanguageConsts.MaxCultureNameLength);
            b.Property(x => x.UiCultureName).IsRequired().HasMaxLength(LanguageConsts.MaxUiCultureNameLength);
            b.Property(x => x.DisplayName).IsRequired().HasMaxLength(LanguageConsts.MaxDisplayNameLength);
            b.Property(x => x.FlagIcon).IsRequired().HasMaxLength(LanguageConsts.MaxFlagIconLength);
            b.Property(x => x.IsEnabled).IsRequired();
        });

        builder.Entity<LanguageText>(b =>
        {
            b.ToTable(LanguageManagementDbProperties.DbTablePrefix + "LanguageTexts", LanguageManagementDbProperties.DbSchema);
            b.ConfigureByConvention();
            b.Property(x => x.ResourceName).IsRequired().HasMaxLength(LanguageTextConsts.MaxResourceNameLength);
            b.Property(x => x.Name).IsRequired().HasMaxLength(LanguageTextConsts.MaxKeyNameLength);
            b.Property(x => x.Value).IsRequired().HasMaxLength(LanguageTextConsts.MaxValueLength);
            b.Property(x => x.CultureName).IsRequired().HasMaxLength(LanguageTextConsts.MaxCultureNameLength);
            b.HasIndex(x => new { x.TenantId, x.ResourceName, x.CultureName });
        });
    }
}
